<template>
    <!-- 基于 Element UI 新增和修改弹窗 -->
    <el-dialog :title="!dataForm.id ? '添加下载文件' : '修改下载文件'" :close-on-click-modal="false" :visible.sync="visible">
        <!-- 新增和创建表单表单 -->
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()"
            label-width="80px">
            <el-form-item label="文件名" prop="name">
                <el-input v-model="dataForm.name" placeholder="文件名"></el-input>
            </el-form-item>
            <el-form-item label="文件地址" prop="fileUrl">
                <!-- <el-input  placeholder="文件地址"></el-input> -->
                <UploadFile v-model="dataForm.fileUrl" @uploadSuccess="fileInput"></UploadFile>
            </el-form-item>
            <el-form-item label="是否显示" prop="show">
                <el-switch v-model="dataForm.show" :active-value="1" :inactive-value="0" active-color="#13ce66"
                    inactive-color="#ff4949">
                </el-switch>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import * as api from '@/api/storeDownload.js'
import UploadFile from '@/components/Upload/uploadFile.vue'

export default {
    components:{ UploadFile },
    data() {
        return {
            visible: false,
            dataForm: {
                id: 0,
                name: '',
                fileUrl: '',
                show: 1,
                createTime: '',
                updateTime: ''
            },
            dataRule: {
                name: [
                    { required: true, message: '文件名为必填项', trigger: 'blur' }
                ],
                fileUrl: [
                    { required: true, message: '文件地址为必填项', trigger: 'blur' }
                ]

            }
        }
    },
    methods: {
        init(id) { // 初始化表单验证规则
            this.dataForm.id = id || 0
            this.visible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {
                    api.storedownloadDetailApi(id).then(res => {
                        this.dataForm = res;
                    })
                }
            })
        },
        fileInput(e){
            this.dataForm.name = e.fileName
        },
        // 表单数据提交
        dataSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    api.StoreDownloadCreateApi(this.dataForm).then(res => {
                        // TODO 保存数据
                        this.$emit('refreshDataList', "form")
                    });
                }
            })
        }
    }
}
</script>